﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace FlowMeter
{
    public partial class MyFlowMeter : UserControl
    {
        private double _minValue = 0;
        private double _maxValue = 100;
        private double _currentValue = 0;
        private string _unit = "L/min";
        private Color _accentColor = Color.FromArgb(41, 128, 185);
        private Color _textColor = Color.FromArgb(52, 73, 94);
        private Color _dialColor = Color.FromArgb(245, 245, 245);
        private int _borderThickness = 0;

        public double MinValue
        {
            get => _minValue;
            set { _minValue = value; Invalidate(); }
        }

        public double MaxValue
        {
            get => _maxValue;
            set { _maxValue = value; Invalidate(); }
        }

        public double CurrentValue
        {
            get => _currentValue;
            set
            {
                _currentValue = Math.Max(_minValue, Math.Min(_maxValue, value));
                Invalidate();
            }
        }

        public string Unit
        {
            get => _unit;
            set { _unit = value; Invalidate(); }
        }

        public Color AccentColor
        {
            get => _accentColor;
            set { _accentColor = value; Invalidate(); }
        }

        public Color TextColor
        {
            get => _textColor;
            set { _textColor = value; Invalidate(); }
        }

        public Color DialColor
        {
            get => _dialColor;
            set { _dialColor = value; Invalidate(); }
        }

        public int BorderThickness
        {
            get => _borderThickness;
            set { _borderThickness = value; Invalidate(); }
        }

        public MyFlowMeter()
        {
            InitializeComponent();
            DoubleBuffered = true;
            Size = new Size(300, 300);
            Font = new Font("Segoe UI", 9, FontStyle.Regular);
            BackColor = Color.Transparent;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

            // 计算仪表盘位置和大小
            int diameter = Math.Min(Width, Height) - 20;
            int x = (Width - diameter) / 2;
            int y = 10;
            Rectangle rect = new Rectangle(x, y, diameter, diameter);
            PointF center = new PointF(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
            float radius = diameter / 2f;

            // 绘制简约表盘
            using (Brush dialBrush = new SolidBrush(_dialColor))
            {
                g.FillEllipse(dialBrush, rect);
            }

            // 绘制刻度
            DrawScales(g, rect, center, radius);

            // 绘制指针
            DrawPointer(g, rect, center, radius);

            // 绘制美化后的数值显示
            DrawValue(g, rect, center);
        }

        private void DrawScales(Graphics g, Rectangle rect, PointF center, float radius)
        {
            int majorTicks = 8;
            float startAngle = 135;
            float sweepAngle = 270;

            // 绘制主刻度线
            for (int i = 0; i <= majorTicks; i++)
            {
                double value = _minValue + (_maxValue - _minValue) * i / majorTicks;
                float angle = startAngle + sweepAngle * i / majorTicks;

                using (Pen pen = new Pen(_textColor, 1.5f))
                {
                    PointF start = GetPointOnCircle(center, radius - 8, angle);
                    PointF end = GetPointOnCircle(center, radius - 2, angle);
                    g.DrawLine(pen, start, end);
                }

                // 刻度值
                if (i > 0 && i < majorTicks)
                {
                    PointF textPoint = GetPointOnCircle(center, radius - 25, angle);
                    using (StringFormat sf = new StringFormat())
                    using (Brush textBrush = new SolidBrush(_textColor))
                    {
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Center;
                        g.DrawString(value.ToString("F0"), Font, textBrush, textPoint, sf);
                    }
                }
            }

            // 绘制最小和最大值
            using (Brush textBrush = new SolidBrush(_textColor))
            {
                PointF minPoint = GetPointOnCircle(center, radius - 25, startAngle);
                PointF maxPoint = GetPointOnCircle(center, radius - 25, startAngle + sweepAngle);

                using (StringFormat sf = new StringFormat())
                {
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    g.DrawString(_minValue.ToString("F0"), Font, textBrush, minPoint, sf);
                    g.DrawString(_maxValue.ToString("F0"), Font, textBrush, maxPoint, sf);
                }
            }
        }

        private void DrawPointer(Graphics g, Rectangle rect, PointF center, float radius)
        {
            float valueRatio = (float)((_currentValue - _minValue) / (_maxValue - _minValue));
            float angle = 135 + 270 * valueRatio;

            // 简约指针
            using (Pen pointerPen = new Pen(_accentColor, 3))
            {
                PointF endPoint = GetPointOnCircle(center, radius - 15, angle);
                g.DrawLine(pointerPen, center, endPoint);
            }

            // 中心点
            using (Brush centerBrush = new SolidBrush(_accentColor))
            {
                g.FillEllipse(centerBrush, center.X - 5, center.Y - 5, 10, 10);
            }
        }

        private void DrawValue(Graphics g, Rectangle rect, PointF center)
        {
            string valueText = $"{_currentValue:F1}";
            string unitText = _unit;

            // 数值显示区域 - 美化设计
            RectangleF valueRect = new RectangleF(
                Width / 2 - 70,
                center.Y + rect.Height * 0.3f,
                140,
                50
            );
            
            // 绘制数值
            using (StringFormat sf = new StringFormat())
            using (Brush textBrush = new SolidBrush(_textColor))
            using (Brush accentBrush = new SolidBrush(_accentColor))
            {
                sf.Alignment = StringAlignment.Center;

                // 数值
                using (Font valueFont = new Font("Segoe UI", 22, FontStyle.Bold))
                {
                    RectangleF numRect = new RectangleF(valueRect.X, valueRect.Y - 35, valueRect.Width, valueRect.Height - 15);
                    g.DrawString(valueText, valueFont, accentBrush, numRect, sf);
                }

                // 单位
                using (Font unitFont = new Font("Segoe UI", 10, FontStyle.Regular))
                {
                    RectangleF unitRect = new RectangleF(valueRect.X, valueRect.Y + valueRect.Height - 45, valueRect.Width, 20);
                    g.DrawString(unitText, unitFont, textBrush, unitRect, sf);
                }
            }
        }

        private PointF GetPointOnCircle(PointF center, float radius, float angle)
        {
            float radians = angle * (float)Math.PI / 180f;
            return new PointF(
                center.X + radius * (float)Math.Cos(radians),
                center.Y + radius * (float)Math.Sin(radians)
            );
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Invalidate();
        }
    }
}
